package com.sh.queue.practive;

/**
 * @ClassName CricleQueueNewDemo
 * @Description TODO
 * @Author yaoqiang
 * @Date 2020/10/26 15:42
 * @Version 1.0
 */
public class CricleQueueNewDemo {
    public static void main(String[] args) {
        CricleQueueNew cir=new CricleQueueNew(4);
        cir.addQueue(10);cir.addQueue(20);cir.addQueue(30);
        cir.showQueue();cir.getQueue();cir.showQueue();cir.addQueue(40);cir.showQueue();
    }
}

class CricleQueueNew {
    private int front,rear,maxSize;
    private int[] arr;
    public  CricleQueueNew(int arrMaxSize){ maxSize=arrMaxSize;arr=new int[maxSize];front=0;rear=0; }

    public boolean isFull(){ return (rear+1)%maxSize==front; }
    public boolean isEmpty(){ return front==rear; }
    public void addQueue(int n){
        if (isFull()) {
            System.out.println("栈已满");
            return;
        }
       arr[rear] =n;
       rear=(rear+1)%maxSize;
    }

    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列空");
        }
        int value=arr[front];
        front=(front+1)%maxSize;
        return value;
    }
    public int size(){ return  (maxSize+rear-front)%maxSize; }

    public void showQueue(){
        for (int i=front;i<front+size();i++){
            System.out.printf("arr[%d]=%d\n",i%maxSize,arr[i%maxSize]);
        }
    }
    public void showHead(){ System.out.println(arr[front]); }
}
